/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Installer.

	Maxsi Installer is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Installer is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Installer. If not, see <http://www.gnu.org/licenses/>.

	Maxsi Installer
	An installer that downloads and sets up software, data, and games.

	PageInstalling.cpp
	The page that is displayed while a product is installed.

******************************************************************************/

#include "MaxsiLibrary.h"
#include "UI.h"
#include "PageInstalling.h"

MaxsiHandle FileHandle	=	NULL;

size_t InstallCallback(BYTE* UserData, IHTTP* Context, BYTE* Buffer, size_t Available)
{
	PrintOutput("Wrote %zu bytes!\n", FileSystem()->WriteFile(FileHandle, Buffer, Available));

	PageInstalling*		Page	=	(PageInstalling*)UserData;

	MESTR*	LabelText;	

	if ( Context->GetResponseSize() == UINT64_MAX )
	{
		LabelText	=	PrintString("Downloaded %zu KiB", (size_t)Context->GetResponseRead()/1024);

		MaxsiWindowEvent	LabelEvent(Page->Window, MAXSI_EVENT_SET_TEXT, (BYTE*)LabelText, MESTRSIZEWN(LabelText));
		MaxsiWindowEvent	ProgressEvent(Page->ProgressBar, MAXSI_EVENT_SET_PROGRESS, (Word_t)100, MAXSI_PROGRESS_INDETERMINATE);

		WindowSystem->PostEventThreaded(&LabelEvent);
		WindowSystem->PostEventThreaded(&ProgressEvent);
	}
	else
	{
		LabelText	=	PrintString("Downloaded %zu KiB of %zu KiB", (size_t)Context->GetResponseRead()/1024, (size_t)Context->GetResponseSize()/1024);

		MaxsiWindowEvent	LabelEvent(Page->Window, MAXSI_EVENT_SET_TEXT, (BYTE*)LabelText, MESTRSIZEWN(LabelText));
		MaxsiWindowEvent	ProgressEvent(Page->ProgressBar, MAXSI_EVENT_SET_PROGRESS, (Word_t)Context->GetResponseRead()*100/Context->GetResponseSize(), MAXSI_PROGRESS_NORMAL);

		WindowSystem->PostEventThreaded(&LabelEvent);
		WindowSystem->PostEventThreaded(&ProgressEvent);
	}

	delete[] LabelText;

	return 0;
}

MaxsiError InstallProduct(size_t ThreadID, BYTE* Parameter)
{
	PageInstalling*		Page	=	(PageInstalling*)Parameter;

	FileHandle		=	FileSystem()->OpenFile(_MESTR("Masix.zip"), MAXSI_FILE_WRITE | MAXSI_FILE_CREATE | MAXSI_FILE_SEQUENTIAL);

	if ( FileHandle == NULL )
	{
		MESTR*	NextText	=	_MESTR("Fial!");

		MaxsiWindowEvent	ButtonEvent(Page->Cancel, MAXSI_EVENT_SET_TEXT, (BYTE*)NextText, MESTRSIZEWN(NextText));
		MaxsiWindowEvent	ProgressEvent(Page->ProgressBar, MAXSI_EVENT_SET_PROGRESS, (Word_t)100.0f, MAXSI_PROGRESS_ERROR);

		WindowSystem->PostEventThreaded(&ButtonEvent);
		WindowSystem->PostEventThreaded(&ProgressEvent);

		return MAXSI_ERROR_COULD_NOT_OPEN;
	}


	IHTTP*	HTTP	=	(IHTTP*)CreateInterface("IHTTP");

	HTTP->SetCallback(InstallCallback);
	HTTP->SetCallbackUserData(Parameter);
	HTTP->SetVersion(1, 1);
	HTTP->SetMethod((ASCII_t*)"GET");
	HTTP->SetHost((ASCII_t*)"www.maxsi.org");
	HTTP->SetPort(80);
	HTTP->SetResource((ASCII_t*)"/http-test/");

	if ( HTTP->Continue() == MAXSI_ERROR_SUCCESS )
	{

		MESTR*	NextText	=	_MESTR("Finish");

		MaxsiWindowEvent	ButtonEvent(Page->Cancel, MAXSI_EVENT_SET_TEXT, (BYTE*)NextText, MESTRSIZEWN(NextText));
		MaxsiWindowEvent	ProgressEvent(Page->ProgressBar, MAXSI_EVENT_SET_PROGRESS, (Word_t)100.0f, MAXSI_PROGRESS_NORMAL);

		WindowSystem->PostEventThreaded(&ButtonEvent);
		WindowSystem->PostEventThreaded(&ProgressEvent);
	}
	else
	{
		MESTR*	NextText	=	_MESTR("Failed!");

		MaxsiWindowEvent	ButtonEvent(Page->Cancel, MAXSI_EVENT_SET_TEXT, (BYTE*)NextText, MESTRSIZEWN(NextText));
		MaxsiWindowEvent	ProgressEvent(Page->ProgressBar, MAXSI_EVENT_SET_PROGRESS, (Word_t)100.0f, MAXSI_PROGRESS_ERROR);

		WindowSystem->PostEventThreaded(&ButtonEvent);
		WindowSystem->PostEventThreaded(&ProgressEvent);
	}

	FileSystem()->CloseFile(FileHandle);

	DeleteInterface(HTTP);

	return MAXSI_ERROR_SUCCESS;
}

PageInstalling::PageInstalling()
{
	Cancel			=	NULL;
	ProgressBar		=	NULL;
}

PageInstalling::~PageInstalling()
{
	
}

MaxsiError PageInstalling::CreateControls()
{
	Cancel			=	Window->CreateChild(_MESTR("Button"), 100, 24, Window->Width()-1*125, Window->Height()-60, _MESTR("Cancel"));
	ProgressBar		=	Window->CreateChild(_MESTR("ProgressBar"), Window->Width() - 50, 24, 25, 100, NULL);
	
	ProgressBar->SetProgress(1.0f, MAXSI_PROGRESS_INDETERMINATE);

	ScheduleWorkerThreadJob(InstallProduct, (BYTE*)this);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError PageInstalling::DeleteControls()
{
	WindowSystem->DeleteWindow(Cancel);
	WindowSystem->DeleteWindow(ProgressBar);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError PageInstalling::OnEvent(MaxsiWindowEvent* Event)
{
	if ( Event->Owner == Cancel && Event->Type == MAXSI_EVENT_PRESSED )
	{
		WindowSystem->ShutDown();
		return MAXSI_ERROR_SUCCESS;
	}

	return MAXSI_ERROR_NOT_SUPPORTED;
}

